package com.material.common.wx.product;

import com.material.common.wx.consts.ApiUrl;
import com.material.common.wx.product.request.ProductDetailRequest;
import com.material.common.wx.product.request.ProductListRequest;
import com.material.common.wx.product.response.ProductDetailResponse;
import com.material.common.wx.product.response.ProductListResponse;
import com.wechat.pay.java.core.http.HttpClient;
import com.wechat.pay.java.core.http.HttpHeaders;
import com.wechat.pay.java.core.http.HttpMethod;
import com.wechat.pay.java.core.http.HttpRequest;
import com.wechat.pay.java.core.http.HttpResponse;
import com.wechat.pay.java.core.http.MediaType;
import com.wechat.pay.java.core.http.RequestBody;
import com.wechat.pay.java.core.util.GsonUtil;

import java.text.MessageFormat;

/**
 * @author ganxinge
 */
public class ProductService {

    private final HttpClient httpClient;

    public ProductService(HttpClient httpClient) {
        this.httpClient = httpClient;
    }

    /**
     * 获取商品列表
     * @param productListRequest
     * @return
     */
    public ProductListResponse getProductList(ProductListRequest productListRequest){
        String accessToken = productListRequest.getAccessToken();
        RequestBody requestBody = (new com.wechat.pay.java.core.http.JsonRequestBody.Builder()).body(GsonUtil.toJson(productListRequest)).build();
        String requestPath = MessageFormat.format(ApiUrl.PRODUCT_LIST,accessToken);
        HttpHeaders headers = new HttpHeaders();
        headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
        headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
        HttpRequest httpRequest = (new com.wechat.pay.java.core.http.HttpRequest.Builder()).httpMethod(HttpMethod.POST).url(requestPath).headers(headers).body(requestBody).build();
        HttpResponse<ProductListResponse> httpResponse = this.httpClient.execute(httpRequest, ProductListResponse.class);
        ProductListResponse serviceResponse = httpResponse.getServiceResponse();
        return serviceResponse;
    }
    /**
     * 获取商品详情
     * @param productDetailRequest
     * @return
     */
    public ProductDetailResponse getProductById(ProductDetailRequest productDetailRequest){
        String accessToken = productDetailRequest.getAccessToken();
        RequestBody requestBody = (new com.wechat.pay.java.core.http.JsonRequestBody.Builder()).body(GsonUtil.toJson(productDetailRequest)).build();
        String requestPath = MessageFormat.format(ApiUrl.PRODUCT_DETAIL,accessToken);
        HttpHeaders headers = new HttpHeaders();
        headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
        headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
        HttpRequest httpRequest = (new com.wechat.pay.java.core.http.HttpRequest.Builder()).httpMethod(HttpMethod.POST).url(requestPath).headers(headers).body(requestBody).build();
        HttpResponse<ProductDetailResponse> httpResponse = this.httpClient.execute(httpRequest, ProductDetailResponse.class);
        return httpResponse.getServiceResponse();
    }



}
